//package com.example.pool.netty;
//
//import io.netty.bootstrap.Bootstrap;
//import io.netty.channel.Channel;
//import io.netty.channel.ChannelFuture;
//import io.netty.channel.ChannelInitializer;
//import io.netty.channel.ChannelOption;
//import io.netty.channel.EventLoopGroup;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.SocketChannel;
//import io.netty.channel.socket.nio.NioSocketChannel;
//import io.netty.handler.codec.http.DefaultFullHttpRequest;
//import io.netty.handler.codec.http.FullHttpRequest;
//import io.netty.handler.codec.http.HttpClientCodec;
//import io.netty.handler.codec.http.HttpContentDecompressor;
//import io.netty.handler.codec.http.HttpHeaderNames;
//import io.netty.handler.codec.http.HttpHeaderValues;
//import io.netty.handler.codec.http.HttpMethod;
//import io.netty.handler.codec.http.HttpObjectAggregator;
//import io.netty.handler.codec.http.HttpResponse;
//import io.netty.handler.codec.http.HttpResponseDecoder;
//import io.netty.handler.codec.http.HttpVersion;
//import io.netty.handler.ssl.SslContext;
//import io.netty.handler.ssl.SslContextBuilder;
//import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
//
//import java.net.URI;
//
//public class NettyHttpClient {
//    private final EventLoopGroup group;
//    private final Bootstrap bootstrap;
//    private final URI uri;
//    private final boolean ssl;
//
//    public NettyHttpClient(String url) throws Exception {
//        this.uri = new URI(url);
//        this.ssl = "https".equalsIgnoreCase(uri.getScheme());
//
//        // 配置客户端
//        group = new NioEventLoopGroup();
//        bootstrap = new Bootstrap()
//                .group(group)
//                .channel(NioSocketChannel.class)
//                .option(ChannelOption.TCP_NODELAY, true)
//                .handler(new ChannelInitializer<SocketChannel>() {
//                    @Override
//                    protected void initChannel(SocketChannel ch) {
//                        if (ssl) {
//                            SslContext sslContext;
//                            try {
//                                sslContext = SslContextBuilder.forClient()
//                                        .trustManager(InsecureTrustManagerFactory.INSTANCE)
//                                        .build();
//                            } catch (Exception e) {
//                                throw new RuntimeException("Error creating SSL context", e);
//                            }
//                            ch.pipeline().addLast(sslContext.newHandler(ch.alloc(), uri.getHost(), uri.getPort()));
//                        }
//                        ch.pipeline().addLast(new HttpClientCodec());
//                        ch.pipeline().addLast(new HttpContentDecompressor());
//                        ch.pipeline().addLast(new HttpObjectAggregator(8192));
//                        ch.pipeline().addLast(new HttpResponseDecoder());
//                    }
//                });
//    }
//
//    public HttpResponse sendGetRequest() throws Exception {
//        // Connect and send the HTTP request
//        Channel channel = bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel();
//        FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
//        request.headers().set(HttpHeaderNames.HOST, uri.getHost());
//        request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
//
//        channel.writeAndFlush(request);
//
//        // Wait for the response
//        ChannelFuture future = channel.closeFuture().sync();
//        return future.syncUninterruptibly().get();
//    }
//
//    public void shutdown() {
//        group.shutdownGracefully();
//    }
//
//    public static void main(String[] args) throws Exception {
//        String url = "http://www.example.com";
//        NettyHttpClient httpClient = new NettyHttpClient(url);
//
//        try {
//            HttpResponse response = httpClient.sendGetRequest();
//            System.out.println("Response: " + response.status());
//            // Process the response content here
//        } finally {
//            httpClient.shutdown();
//        }
//    }
//}
//
